package com.robotserver.orders.jobs.xxljob;

import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.constant.CacheKey;
import com.robotserver.orders.param.TbOrderRefundSyncParam;
import com.robotserver.orders.service.TbOrderRefundService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TbOrderRefundSyncXxlJob {
    private static Logger logger = LoggerFactory.getLogger(TbOrderRefundSyncXxlJob.class);

    @Autowired
    private TbOrderRefundService tbOrderRefundService;
    /**
     * 生产淘宝维权订单的tokens
     * 每20秒钟执行一次拉取订单（时间范围是5分钟）
     */
    @XxlJob("pushTbOrdersRefundHander_producer")
    public void pushTbOrdersRefundHander_producer() {
        logger.info("--------TbOrderRefundSyncXxlJob pushTbOrdersRefundHander_producer------start------");
        //默认持续时间为5分钟
        Long duration = 300L;
        String durationStr = "";
        try {
            //接受参数-拉取当前日期往前的几秒订单
            durationStr = XxlJobHelper.getJobParam();
            if(StringUtils.isNotBlank(durationStr)){
                //从XXL-Job接受参数
                duration = Long.parseLong(durationStr);
            }
        }catch (Exception e){
            logger.error("xxl-job para set is error {}:"+durationStr);
            //默认设置一分钟
            duration = 300L;
        }
        Integer tmptime = DateUtil.getNowTime();
        Long end_time = Long.valueOf(tmptime);                //截至时间为结束时间
        Long begin_time = tmptime - duration;                 //开始时间为当前之间减去持续时间

        //维权渠道订单
        TbOrderRefundSyncParam tbOrdersRefundSyncParam_channel = new TbOrderRefundSyncParam();
        tbOrdersRefundSyncParam_channel.setBegin_time(begin_time);
        tbOrdersRefundSyncParam_channel.setEnd_time(end_time);
        tbOrdersRefundSyncParam_channel.setType(5);                 //5-维权订单更新时间
        tbOrdersRefundSyncParam_channel.setBizType(1L);             //1代表渠道订单
        tbOrdersRefundSyncParam_channel.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_channel);

        //新版全量下载维权订单接口，故调用一次就行
        //维权会员订单
//        TbOrderRefundSyncParam tbOrdersRefundSyncParam_sid = new TbOrderRefundSyncParam();
//        tbOrdersRefundSyncParam_sid.setBegin_time(begin_time);
//        tbOrdersRefundSyncParam_sid.setEnd_time(end_time);
//        tbOrdersRefundSyncParam_sid.setType(5);                 //5-维权订单更新时间
//        tbOrdersRefundSyncParam_sid.setBizType(2L);             //2代表会员订单
//        tbOrdersRefundSyncParam_sid.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
//        //调用生产者线程，遍历联盟，往redis队列中添加alimmTokens
//        tbOrderRefundService.syncOrder_producer(tbOrdersRefundSyncParam_sid);

        logger.info("-------------往redis中生产淘宝联盟维权订单------");
    }

    /**
     * 消费淘宝联盟渠道tokens拉取渠道订单信息
     * 每30秒钟执行一次拉取订单（时间范围是3分钟）
     */
    @XxlJob("pushTbOrdersRefundHander_consumer")
    public void pushTbOrdersRefundHander_consumer() {
        logger.info("--------TbOrderChannelSyncXxlJob pushTbOrdersRefundHander_consumer------start------");
        //调用消费者线程，遍历redis队列，消费alimmTokens
        TbOrderRefundSyncParam tbOrdersRefundSyncParam = new TbOrderRefundSyncParam();
        tbOrdersRefundSyncParam.setRedisQueueKey(CacheKey.tbOrderRefundQueue);
        tbOrderRefundService.syncOrder_consumer(tbOrdersRefundSyncParam);
        logger.info("---------------------------消费淘宝联盟的维权订单-----------------");
    }

}
